package com.cebbank.yyi.springbootapp.jingtang.controller;

import cn.hutool.core.util.StrUtil;
import com.cebbank.yyi.springbootapp.jingtang.dao.RcDocDao;
import com.cebbank.yyi.springbootapp.jingtang.dto.ConditionParams;
import com.cebbank.yyi.springbootapp.jingtang.dto.DbMigrationParams;
import com.cebbank.yyi.springbootapp.jingtang.dto.FileOperatorParams;
import com.cebbank.yyi.springbootapp.jingtang.json.EsBO;
import com.cebbank.yyi.springbootapp.jingtang.json.MenuBO;
import com.cebbank.yyi.springbootapp.jingtang.service.SystemMigrationService;
import com.cebbank.yyi.springbootapp.jingtang.test.ApplyMappingService;
import com.cebbank.yyi.springbootapp.jingtang.test.DocFileType;
import com.cebbank.yyi.springbootapp.jingtang.test.ResponseDTO;
import com.cebbank.yyi.springbootapp.jingtang.utils.FileUtil;
import com.cebbank.yyi.springbootapp.jingtang.utils.RequestUtil;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 文档附件列表(RcDocAttachment)表控制层
 *
 * @author makejava
 * @since 2024-08-22 10:52:58
 */
@RestController
@RequestMapping("sysMigration")
public class SystemMigrationController {

    @Autowired
    private SystemMigrationService systemMigrationService;


    @Autowired
    private ApplyMappingService applyMappingService;

    @Autowired
    private RcDocDao rcDocDao;


    @PostMapping("dbMigration")
    public void dbMigration(@RequestBody DbMigrationParams dbMigrationParams) throws Exception {

        systemMigrationService.dbMigration(dbMigrationParams);
    }

    @PostMapping("dbMigrationForRecord")
    public void dbMigrationForRecord(@RequestBody DbMigrationParams dbMigrationParams) throws Exception {

        systemMigrationService.dbMigrationForRecord(dbMigrationParams);
    }

    @PostMapping("downloadAnnexFileFiles")
    public void downloadAnnexFileFiles(@RequestBody DbMigrationParams dbMigrationParams) throws Exception {

        systemMigrationService.dbMigration(dbMigrationParams);
    }

    @PostMapping("test")
    public String dbMi() throws Exception {

        String str = "{\n" + "    \"code\": 0,\n" + "    \"msg\": null,\n" + "    \"data\": {\n" + "        \"listSearch\": [\n" + "            {\n" + "                \"value\": \"3\",\n" + "                \"label\": \"执行中\",\n" + "                \"sort\": 0,\n" + "                \"count\": 4\n" + "            },\n" + "            {\n" + "                \"value\": \"91\",\n" + "                \"label\": \"未读\",\n" + "                \"sort\": 1,\n" + "                \"count\": 0\n" + "            },\n" + "            {\n" + "                \"value\": \"92\",\n" + "                \"label\": \"已读\",\n" + "                \"sort\": 2,\n" + "                \"count\": 0\n" + "            },\n" + "            {\n" + "                \"value\": \"2\",\n" + "                \"label\": \"编审批中\",\n" + "                \"sort\": 5,\n" + "                \"count\": 0\n" + "            },\n" + "            {\n" + "                \"value\": \"5\",\n" + "                \"label\": \"废止\",\n" + "                \"sort\": 6,\n" + "                \"count\": 2\n" + "            },\n" + "            {\n" + "                \"value\": \"99\",\n" + "                \"label\": \"我的新增\",\n" + "                \"sort\": 7,\n" + "                \"count\": 0\n" + "            }\n" + "        ],\n" + "        \"page\": {\n" + "            \"records\": [\n" + "                {\n" + "                    \"id\": 15341,\n" + "                    \"fileName\": \"运营管理手册2024.3\",\n" + "                    \"fileLastId\": 8539,\n" + "                    \"filePath\": \"/doc/sfiles/download/100005\",\n" + "                    \"fileUploadTip\": \"100083\",\n" + "                    \"originalFileId\": \"100005\",\n" + "                    \"pdfFileId\": null,\n" + "                    \"departmentId\": \"\",\n" + "                    \"deptName\": \"制造部\",\n" + "                    \"workingId\": \"\",\n" + "                    \"workingName\": null,\n" + "                    \"productLine\": \"\",\n" + "                    \"regimePropertyId\": \"\",\n" + "                    \"fileNo\": \"SJTGLSC\",\n" + "                    \"dataForZNBMLX\": \"\",\n" + "                    \"dataForZYBMLX\": \"\",\n" + "                    \"fileType\": \"\",\n" + "                    \"docType\": null,\n" + "                    \"edition\": \"VN184654e1d2f\",\n" + "                    \"editionNo\": \"18e2c25e4d7\",\n" + "                    \"fileVersion\": \"2\",\n" + "                    \"effectDate\": \"2024-03-11\",\n" + "                    \"destroyFileId\": \"\",\n" + "                    \"destroyFileName\": null,\n" + "                    \"releaseDate\": \"2024-03-11\",\n" + "                    \"discardDate\": null,\n" + "                    \"deleteList\": null,\n" + "                    \"simulationDraftId\": null,\n" + "                    \"approveE\": null,\n" + "                    \"approveDate\": null,\n" + "                    \"firstLevel\": \"\",\n" + "                    \"secrecyLevel\": \"2\",\n" + "                    \"secrecyTerm\": \"5\",\n" + "                    \"simulationDraft\": \"沈忱,李顺心,张静,张振兴,徐华\",\n" + "                    \"formulateDepartmentId\": 20001488,\n" + "                    \"formulateDepName\": \"制造部\",\n" + "                    \"iscontolYear\": \"1\",\n" + "                    \"rulesId\": \"\",\n" + "                    \"rules\": \"\",\n" + "                    \"rulesNo\": \"\",\n" + "                    \"viewRange\": null,\n" + "                    \"viewPersonnel\": null,\n" + "                    \"downloadRange\": null,\n" + "                    \"downloadPersonnel\": null,\n" + "                    \"status\": 3,\n" + "                    \"readStatus\": \"2\",\n" + "                    \"readStatusDesc\": \"已读\",\n" + "                    \"approveFlowId\": \"1d81ce16-4189-4bf2-84cd-218f622aaed9\",\n" + "                    \"discardFlowId\": null,\n" + "                    \"workFlowApprove\": \"glsc_002\",\n" + "                    \"workFlowDiscard\": \"\",\n" + "                    \"unscramblePath\": null,\n" + "                    \"unscrambleCode\": null,\n" + "                    \"processSetId\": null,\n" + "                    \"unscrambleName\": null,\n" + "                    \"unscrambleText\": null,\n" + "                    \"delFlg\": 0,\n" + "                    \"uploader\": \"10012538\",\n" + "                    \"insUserId\": 10012538,\n" + "                    \"insDate\": \"2023-10-30 15:31:22\",\n" + "                    \"updUserId\": 8649,\n" + "                    \"updDate\": \"2024-03-11 14:18:00\",\n" + "                    \"dateTypeColor\": 4,\n" + "                    \"tenantId\": 1,\n" + "                    \"menuType\": 2,\n" + "                    \"submenuType\": \"\",\n" + "                    \"professionPropertyId\": null,\n" + "                    \"viewProductionLineId\": null,\n" + "                    \"viewDepartmentId\": null,\n" + "                    \"suggOrQuestion\": null,\n" + "                    \"improveType\": null,\n" + "                    \"uuId\": \"f731e42a15a94be382327cace5d72d7c\",\n" + "                    \"versionUrl\": null,\n" + "                    \"newFileType\": null,\n" + "                    \"versionType\": null,\n" + "                    \"keywordList\": null,\n" + "                    \"relationInfos\": null,\n" + "                    \"annexInfoList\": null,\n" + "                    \"versionList\": null,\n" + "                    \"fileLastName\": null,\n" + "                    \"processSetName\": null,\n" + "                    \"processSetStr\": null,\n" + "                    \"regimePropertyName\": null,\n" + "                    \"dateS\": null,\n" + "                    \"dateE\": null,\n" + "                    \"editionSerial\": \"V2\",\n" + "                    \"userName\": \"10084893\",\n" + "                    \"userDate\": null,\n" + "                    \"sysDepts\": null,\n" + "                    \"fileProblemList\": null,\n" + "                    \"approveType\": null,\n" + "                    \"fileId\": null,\n" + "                    \"statusName\": \"执行/已读\",\n" + "                    \"remake\": null,\n" + "                    \"filennexInfo\": null,\n" + "                    \"fileAnnexName\": null,\n" + "                    \"busiId\": null,\n" + "                    \"examineUser\": null,\n" + "                    \"approvalUser\": null,\n" + "                    \"fileSource\": null,\n" + "                    \"fileTypeValue\": null,\n" + "                    \"rulesName\": null,\n" + "                    \"checkDtos\": null,\n" + "                    \"abandonedInfos\": null,\n" + "                    \"dueDate\": null,\n" + "                    \"dueDateDesc\": \"将于2025年03月11日过期\",\n" + "                    \"fileTypeStr\": \"管理手册\",\n" + "                    \"professionName\": null,\n" + "                    \"firstLevelStr\": null,\n" + "                    \"secrecyLevelStr\": null,\n" + "                    \"secrecyTermStr\": null,\n" + "                    \"submitDate\": \"2024-02-05\",\n" + "                    \"auditDate\": \"2024-03-11 15:15:05\",\n" + "                    \"preparedBy\": null,\n" + "                    \"standardChineseDescription\": null,\n" + "                    \"standardNumber\": null,\n" + "                    \"instituteCode\": null,\n" + "                    \"active\": null,\n" + "                    \"blockFlag\": null,\n" + "                    \"mainBody\": null,\n" + "                    \"versionNo\": null,\n" + "                    \"chineseDesc\": null,\n" + "                    \"mainBodyVersion\": null,\n" + "                    \"docTitle\": \"运营管理手册2024.3\",\n" + "                    \"professionalReview\": null,\n" + "                    \"standardizInspection\": null,\n" + "                    \"preparedByStr\": null,\n" + "                    \"menu\": null,\n" + "                    \"collect\": 2,\n" + "                    \"flowApproveList\": null,\n" + "                    \"departmentIds\": null,\n" + "                    \"flowDiscardList\": null,\n" + "                    \"taskAuditUser\": null,\n" + "                    \"taskNullifyUser\": null,\n" + "                    \"jumpUrl\": \"/manageDOC/manageManual/\",\n" + "                    \"level\": null,\n" + "                    \"preparedBy1\": null,\n" + "                    \"preparedUserInfo\": \"[{\\\"postSid\\\":\\\"20001492\\\",\\\"jobName\\\":\\\"安全制度管理\\\",\\\"mobilePhone\\\":\\\"15832557855\\\",\\\"userNo\\\":\\\"10010179\\\",\\\"postName\\\":\\\"安全管理部\\\",\\\"jobCode\\\":\\\"20001492993\\\",\\\"orgSid\\\":\\\"20001492\\\",\\\"orgFullName\\\":\\\"安全管理部\\\",\\\"userName\\\":\\\"沈忱\\\"},{\\\"postSid\\\":\\\"20001559\\\",\\\"jobName\\\":\\\"环境体系兼清洁生产管理\\\",\\\"mobilePhone\\\":\\\"15832552415\\\",\\\"userNo\\\":\\\"10009609\\\",\\\"postName\\\":\\\"环境保护处\\\",\\\"jobCode\\\":\\\"20493864004\\\",\\\"orgSid\\\":\\\"20001485\\\",\\\"orgFullName\\\":\\\"能源与环境部\\\",\\\"userName\\\":\\\"李顺心\\\"},{\\\"postSid\\\":\\\"20001560\\\",\\\"jobName\\\":\\\"能源统计与计划管理\\\",\\\"mobilePhone\\\":\\\"15832552152\\\",\\\"userNo\\\":\\\"10009613\\\",\\\"postName\\\":\\\"能源技术室\\\",\\\"jobCode\\\":\\\"20493846008\\\",\\\"orgSid\\\":\\\"20001485\\\",\\\"orgFullName\\\":\\\"能源与环境部\\\",\\\"userName\\\":\\\"张静\\\"},{\\\"postSid\\\":\\\"20001619\\\",\\\"jobName\\\":\\\"标准计量主管\\\",\\\"mobilePhone\\\":\\\"15832552283\\\",\\\"userNo\\\":\\\"10011790\\\",\\\"postName\\\":\\\"钢铁联合有限责任公司信息计量部标准计量室\\\",\\\"jobCode\\\":\\\"20001619993\\\",\\\"orgSid\\\":\\\"20001491\\\",\\\"orgFullName\\\":\\\"数智运营部\\\",\\\"userName\\\":\\\"张振兴\\\"},{\\\"postSid\\\":\\\"20001589\\\",\\\"jobName\\\":\\\"质量体系管理\\\",\\\"mobilePhone\\\":\\\"15832557305\\\",\\\"userNo\\\":\\\"10012538\\\",\\\"postName\\\":\\\"技术管理室\\\",\\\"jobCode\\\":\\\"20001589991\\\",\\\"orgSid\\\":\\\"20001488\\\",\\\"orgFullName\\\":\\\"制造部\\\",\\\"userName\\\":\\\"徐华\\\"}]\",\n" + "                    \"issuer\": null,\n" + "                    \"issuer1\": null,\n" + "                    \"issUserInfo\": \"[{\\\"postSid\\\":\\\"99990000\\\",\\\"jobName\\\":\\\"党委副书记、董事、总经理\\\",\\\"mobilePhone\\\":\\\"15832550208\\\",\\\"userNo\\\":\\\"10016309\\\",\\\"postName\\\":\\\"公司领导\\\",\\\"jobCode\\\":\\\"20000062860\\\",\\\"orgSid\\\":\\\"20607402\\\",\\\"orgFullName\\\":\\\"公司领导\\\",\\\"userName\\\":\\\"李明\\\"}]\",\n" + "                    \"basis\": \"根据部门机构调整、外审问题和制度修订进行修订\",\n" + "                    \"reviewer\": \"周建,吴礼云\",\n" + "                    \"reviewerUserInfo\": \"[{\\\"postSid\\\":\\\"99990000\\\",\\\"jobName\\\":\\\"副总经理\\\",\\\"mobilePhone\\\":\\\"15832551958\\\",\\\"userNo\\\":\\\"10010867\\\",\\\"postName\\\":\\\"公司领导\\\",\\\"jobCode\\\":\\\"20000062993\\\",\\\"orgSid\\\":\\\"20607402\\\",\\\"orgFullName\\\":\\\"公司领导\\\",\\\"userName\\\":\\\"周建\\\"},{\\\"postSid\\\":\\\"99990000\\\",\\\"jobName\\\":\\\"副总经理\\\",\\\"mobilePhone\\\":\\\"15832552001\\\",\\\"userNo\\\":\\\"10009623\\\",\\\"postName\\\":\\\"公司领导\\\",\\\"jobCode\\\":\\\"20000062993\\\",\\\"orgSid\\\":\\\"20607402\\\",\\\"orgFullName\\\":\\\"公司领导\\\",\\\"userName\\\":\\\"吴礼云\\\"}]\",\n" + "                    \"paperwork\": null,\n" + "                    \"paperworkUserInfo\": null,\n" + "                    \"category\": null,\n" + "                    \"responsibleDepartment\": null,\n" + "                    \"specialPlan\": null,\n" + "                    \"admin\": null,\n" + "                    \"reviewer1\": null,\n" + "                    \"issuer2\": null,\n" + "                    \"preparedBy2\": null,\n" + "                    \"submitFlowId\": null,\n" + "                    \"submitWorkFlowApprove\": null,\n" + "                    \"flowSubmitList\": null,\n" + "                    \"taskSubmitUser\": null,\n" + "                    \"rulesDtos\": null,\n" + "                    \"flagValue\": null,\n" + "                    \"flagName\": null,\n" + "                    \"nextSubmitNodeKey\": null,\n" + "                    \"nextUserInfos\": null,\n" + "                    \"isPublish\": false,\n" + "                    \"viewPermissions\": null,\n" + "                    \"downloadPermissions\": null,\n" + "                    \"dataForisCurle\": null,\n" + "                    \"unUserfulResult\": null,\n" + "                    \"solidDoc\": null,\n" + "                    \"changeType\": null,\n" + "                    \"isProductPermissions\": null,\n" + "                    \"topCateId\": null,\n" + "                    \"topCateNameStr\": null,\n" + "                    \"topCateName\": null,\n" + "                    \"dutyDepartmentId\": null,\n" + "                    \"dutyDepartmentName\": null,\n" + "                    \"reviewChange\": \"\",\n" + "                    \"proReviewChange\": \"\",\n" + "                    \"submitType\": null,\n" + "                    \"reviseWorkFlowApprove\": \"\",\n" + "                    \"reviseWorkFlowId\": \"\",\n" + "                    \"nullifyPaperworks\": null,\n" + "                    \"nullifyPaperwork\": \"\",\n" + "                    \"nullifyExplain\": \"\",\n" + "                    \"belongFileName\": null,\n" + "                    \"belongFilePath\": null,\n" + "                    \"annexStr\": null,\n" + "                    \"mediumStr\": null,\n" + "                    \"mediumName\": null,\n" + "                    \"mediumType\": null,\n" + "                    \"belongFileNo\": null,\n" + "                    \"instituteNameList\": null,\n" + "                    \"instituteNameStr\": null,\n" + "                    \"instituteCodeStr\": null,\n" + "                    \"companyDirector\": null,\n" + "                    \"companyDirectorUserInfo\": null,\n" + "                    \"mediumLocation\": null,\n" + "                    \"archiveDateFrom\": null,\n" + "                    \"rulesMenutype\": null,\n" + "                    \"isRulesCompany\": null,\n" + "                    \"phone\": null,\n" + "                    \"isOwnDepartment\": false,\n" + "                    \"proReviewChange1\": null,\n" + "                    \"changeTypeStr\": \"部门内部\",\n" + "                    \"pilotTypeStr\": null,\n" + "                    \"applyDeptNames\": null,\n" + "                    \"applyDeptDto\": null,\n" + "                    \"applyDeptDtoShow\": null,\n" + "                    \"rulesPath\": null,\n" + "                    \"rulesUploadTip\": null,\n" + "                    \"isOwnUser\": false,\n" + "                    \"professionalReviewName\": null,\n" + "                    \"docTypeName\": null,\n" + "                    \"rulesJumpUrl\": null,\n" + "                    \"fileChanged\": false,\n" + "                    \"insDateStr\": \"2023-10-30\",\n" + "                    \"auditDateStr\": \"2024-03-11\",\n" + "                    \"discardDateStr\": \"\",\n" + "                    \"submitDateStr\": \"2024-02-05\",\n" + "                    \"effectDateStr\": \"2024-03-11\",\n" + "                    \"approveDateStr\": \"\",\n" + "                    \"releaseDateStr\": \"2024-03-11\",\n" + "                    \"dueDateStr\": \"\"\n" + "                }\n" + "            ],\n" + "            \"total\": 4,\n" + "            \"size\": 1,\n" + "            \"current\": 1,\n" + "            \"orders\": [],\n" + "            \"hitCount\": false,\n" + "            \"searchCount\": true,\n" + "            \"pages\": 4\n" + "        }\n" + "    }\n" + "}";
//        applyMappingService.applyMapping(str, "", DocType.MANAGE_MANUAL);

        return "SUCCESS";
    }

    @PostMapping("uploadMainFile")
    public void uploadMainFile(@RequestBody FileOperatorParams fileOperatorParams) {
        systemMigrationService.uploadMainFile(fileOperatorParams);
    }

    @PostMapping("uploadAttachmentFile")
    public void uploadAttachmentFile(@RequestBody FileOperatorParams fileOperatorParams) {
//        FileUtil.uploadAttachmentFile(fileOperatorParams);
    }

    @PostMapping("docEsSync")
    public void docEsSync(@RequestBody FileOperatorParams fileOperatorParams) {
        FileUtil.docEsSync(fileOperatorParams);
    }


    @PostMapping("selectAll")
    public void selectAll() {
        applyMappingService.selectAll();
    }


    @PostMapping("deleteAll")
    public void deleteAll(@RequestBody FileOperatorParams fileOperatorParams) {
        for (String versionSuffix : fileOperatorParams.getPrefixSuffix()) {
            System.err.printf("开始删除ID后缀为：[%s]的文件\n", versionSuffix);
            applyMappingService.deleteAll(versionSuffix);

        }
    }
//    @PostMapping("upload")
//    public void upload

    @GetMapping("/getInfo")
    public String getInfo(@RequestParam("id") String id, @RequestParam("token") String token) {
        String approvalRecord = RequestUtil.getApprovalRecord(id, token);
        System.err.println("请求到的数据:" + approvalRecord);
        return approvalRecord;
    }

    @PostMapping("/deleteByMenuType")
    public String deleteByMenuType(@RequestBody DbMigrationParams dbMigrationParams) throws Exception {
        List<DocFileType> docFileTypes = dbMigrationParams.getDocFileTypes();
        List<String> statusType = Arrays.asList("3", "5");
        for (DocFileType docFileType : docFileTypes) {
            if (docFileType.getMenuType() == null) {
                continue;
            }
            // 遍历运行中和已废弃
            for (String status : statusType) {
                dbMigrationParams.setStatus(status);
                // 存放当前类型已同步成功的rdDocId
                List<String> successProcessList = new ArrayList<>();
                // 接口需要上送的参数，比如分页参数
                if (docFileType.getMenuType() == null) {
                    continue;
                }
                System.out.printf("删除文件", docFileType.getMenuType(), status);
                ConditionParams conditionParams = new ConditionParams();
                MenuBO menuBO = new MenuBO();
                menuBO.setMenuType(docFileType.getMenuType().toString());
                menuBO.setType(docFileType.getType());
                menuBO.setName(docFileType.getName());
                conditionParams.setPageSize(1);
                //该位置要改动
                int pageNo = 0;
                boolean isStop = false;
                while (!isStop) {
                    pageNo += 1;
                    conditionParams.setPageNo(pageNo);
                    String json = RequestUtil.toRequest(dbMigrationParams, menuBO, conditionParams);
                    Gson gson = new Gson();
                    ResponseDTO testBean = gson.fromJson(json, ResponseDTO.class);
                    if (testBean.getData() == null) {
                        System.err.println("获取的data域为null" + testBean);
                        return null;
                    }
                    List<ResponseDTO.DataDTO.PageDTO.RecordsDTO> records = testBean.getData().getPage().getRecords();
                    for (ResponseDTO.DataDTO.PageDTO.RecordsDTO record : records) {
                        String rcId = record.getId() + FileUtil.getVersion();
                        rcDocDao.deleteById(rcId);
                        try {
                            String esInfo = RequestUtil.getEsDocById(rcId);
                            System.err.println(esInfo);
                            if (StrUtil.isEmpty(esInfo)) {
                                System.err.printf("当前docId[%s]的ES信息为空%n", rcId);
                                continue;
                            }
                            EsBO esBO = gson.fromJson(esInfo, EsBO.class);
                            String esId = esBO.getId();
                            String delInfo = RequestUtil.deleteEsById(esId);
                            System.err.println("删除ES信息结果，delInfo:" + delInfo);
                        } catch (Exception e) {
                            System.err.println("删除es错误:" + e);
                        }
                    }
                    if (testBean.getData().getPage().getRecords().isEmpty()) {
                        isStop = true;
                    }
                }
            }

        }
        return "SUCCESS";
    }

}

